package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.objects.Card;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;

@SuppressLint("SimpleDateFormat")
public class GetCreditCardListWSControl extends BaseWSControl {
	private String stripeId;

	public GetCreditCardListWSControl(Context context,
			WebServiceCommunicatorListener listener, String stripeId) {
		super(context, listener, WebServiceFlag.GET_CREDIT_CARDS);
		this.stripeId = stripeId;

	}

	@Override
	protected Object doInBackground(Void... params) {
		// Get local database

		String url = Statics.WASHIO_WS_URL + "payment/customer/" + stripeId;

		try {
			String respone = NetworkUtils.doRequest(url);
			try {
				JSONObject jsonObject = new JSONObject(respone);
				if (jsonObject.has("error")) {
					mError = jsonObject.getString("error");
					return null;
				} else {
					return getCreditCards(jsonObject.getJSONObject("customer")
							.getJSONObject("cards").getJSONArray("data"));
				}

			} catch (JSONException e) {
				mError = e.getMessage();
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<Card> getCreditCards(JSONArray cardJSONArray)
			throws JSONException {
		ArrayList<Card> creditCards = new ArrayList<Card>();
		Card card;
		for (int i = 0; i < cardJSONArray.length(); i++) {
			JSONObject cardJSONObject = cardJSONArray.getJSONObject(i);
			card = new Card();
			card.setId(cardJSONObject.getString("id"));
			card.setLast4(cardJSONObject.getString("last4"));
			card.setType(cardJSONObject.getString("type"));
			card.setExpMonth(cardJSONObject.getInt("exp_month"));
			card.setExpYear(cardJSONObject.getInt("exp_year"));
			card.setFingerPrint(cardJSONObject.getString("fingerprint"));
			card.setCustomer(cardJSONObject.getString("customer"));
			card.setCountry(cardJSONObject.getString("country"));
			card.setName(cardJSONObject.getString("name"));
			card.setAddressLine1(cardJSONObject.getString("address_line1"));
			card.setAddressLine2(cardJSONObject.getString("address_line2"));
			card.setAddressCity(cardJSONObject.getString("address_city"));
			card.setAddressState(cardJSONObject.getString("address_state"));
			card.setAddressZip(cardJSONObject.getString("address_zip"));
			card.setAddressCountry(cardJSONObject.getString("address_country"));
			card.setCvcCheck(cardJSONObject.getString("cvc_check"));
			card.setAddressLine1Check(cardJSONObject
					.getString("address_line1_check"));
			card.setAddressZipCheck(cardJSONObject
					.getString("address_zip_check"));

			creditCards.add(card);

		}
		return creditCards;
	}
}
